﻿using CPPEI.ETL.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain
{
    public class EtlDbContext : DbContext
    {
        private DbContextTransaction _transaction;
        static EtlDbContext()
        {
            Database.SetInitializer<EtlDbContext>(null);
        }

        public EtlDbContext()
            : base("name=etl-db")
        {
            // _transaction = this.Database.BeginTransaction();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Entities.Log>().ToTable("ETL_com_log");
            modelBuilder.Entity<Entities.Log>().HasKey(p => p.Id);

            modelBuilder.Entity<DtSer>().ToTable("ETL_DtSer");
            modelBuilder.Entity<DtSer>().HasKey(p => p.Id);

            modelBuilder.Entity<DtSource>().ToTable("ETL_DtSource");
            modelBuilder.Entity<DtSource>().HasKey(p => p.Id);

            modelBuilder.Entity<EDtSourceColumn>().ToTable("ETL_DtSourceColumn");
            modelBuilder.Entity<EDtSourceColumn>().HasKey(p => p.Id);

            modelBuilder.Entity<EDtSourceParameter>().ToTable("ETL_DtSourceParameter");
            modelBuilder.Entity<EDtSourceParameter>().HasKey(p => p.Id);

            modelBuilder.Entity<DtSerType>().ToTable("ETL_DtSerType");
            modelBuilder.Entity<DtSerType>().HasKey(p => p.Id);

            modelBuilder.Entity<DtSerCategory>().ToTable("ETL_DtSerCategory");
            modelBuilder.Entity<DtSerCategory>().HasKey(p => p.Id);

            modelBuilder.Entity<DtSerFolder>().ToTable("ETL_DtSerFolder");
            modelBuilder.Entity<DtSerFolder>().HasKey(p => p.Id);

            modelBuilder.Entity<EDtSourceMapping>().ToTable("ETL_DtSourceMapping");
            modelBuilder.Entity<EDtSourceMapping>().HasKey(p => new { p.DtSerId, p.DtSourceId, p.DtTableId });

            modelBuilder.Entity<EDtSourceColumnMapping>().ToTable("ETL_DtSourceColumnMapping");
            modelBuilder.Entity<EDtSourceColumnMapping>().HasKey(p => new { p.DtSerId, p.DtSourceId, p.DtSourceColumnId, p.DtTableId, p.DtTableColumnId });

            modelBuilder.Entity<EDtTable>().ToTable("ETL_DtTable");
            modelBuilder.Entity<EDtTable>().HasKey(p => p.Id);

            modelBuilder.Entity<EDtTableColumn>().ToTable("ETL_DtTableColumn");
            modelBuilder.Entity<EDtTableColumn>().HasKey(p => p.Id);

            modelBuilder.Entity<EDtSourceDataIdentity>().ToTable("ETL_DtSourceDataIdentity");
            modelBuilder.Entity<EDtSourceDataIdentity>().HasKey(p => new { p.DtSerId, p.DtSourceId, p.DtSourceColumnId, p.DtTableId });

            modelBuilder.Entity<EDim>().ToTable("ETL_Dim");
            modelBuilder.Entity<EDim>().HasKey(p => p.Id);

            modelBuilder.Entity<EDimItem>().ToTable("ETL_DimItem");
            modelBuilder.Entity<EDimItem>().HasKey(p => p.Id);

            modelBuilder.Entity<ECodeConversionRule>().ToTable("ETL_CodeConversionRule");
            modelBuilder.Entity<ECodeConversionRule>().HasKey(p => p.Id);

            modelBuilder.Entity<EDataVerificationRule>().ToTable("ETL_DataVerificationRule");
            modelBuilder.Entity<EDataVerificationRule>().HasKey(p => p.Id);

            modelBuilder.Entity<EDVRParameter>().ToTable("ETL_DVRParameter");
            modelBuilder.Entity<EDVRParameter>().HasKey(p => p.Id);

            modelBuilder.Entity<ETask>().ToTable("ETL_Task");
            modelBuilder.Entity<ETask>().HasKey(p => p.Id);
            modelBuilder.Entity<ETaskExecLog>().ToTable("ETL_TaskExecLog");
            modelBuilder.Entity<ETaskExecLog>().HasKey(p => p.Id);
            modelBuilder.Entity<ETaskCategory>().ToTable("ETL_TaskCategory");
            modelBuilder.Entity<ETaskCategory>().HasKey(p => p.Id);

            modelBuilder.Entity<ETaskParameter>().ToTable("ETL_TaskParameter");
            modelBuilder.Entity<ETaskParameter>().HasKey(p => p.Id);

            modelBuilder.Entity<ETaskRunMode>().ToTable("ETL_TaskRunMode");
            modelBuilder.Entity<ETaskRunMode>().HasKey(p => p.Id);

            modelBuilder.Entity<ETaskExecuteRecord>().ToTable("ETL_TaskExecuteRecord");
            modelBuilder.Entity<ETaskExecuteRecord>().HasKey(p => p.Id);

            modelBuilder.Entity<EAppAuthorization>().ToTable("ETL_AppAuthorization");
            modelBuilder.Entity<EAppAuthorization>().HasKey(p => p.Id);

            modelBuilder.Entity<EDtTableDataHistory>().ToTable("ETL_DtTableDataHistory");
            modelBuilder.Entity<EDtTableDataHistory>().HasKey(p => p.Id);

            modelBuilder.Entity<EBatch>().ToTable("ETL_Batch");
            modelBuilder.Entity<EBatch>().HasKey(p => p.Id);

            modelBuilder.Entity<EBatchParameter>().ToTable("ETL_BatchParameter");
            modelBuilder.Entity<EBatchParameter>().HasKey(p => p.Id);

            modelBuilder.Entity<ERawDataExtractHistory>().ToTable("ETL_RawDataExtractHistory");
            modelBuilder.Entity<ERawDataExtractHistory>().HasKey(p => p.Id);

            modelBuilder.Entity<EDVPlugInAssembly>().ToTable("ETL_DVPlugInAssembly");
            modelBuilder.Entity<EDVPlugInAssembly>().HasKey(p => p.Id);

            modelBuilder.Entity<EDVPlugIn>().ToTable("ETL_DVPlugIn");
            modelBuilder.Entity<EDVPlugIn>().HasKey(p => p.Id);

            modelBuilder.Entity<EExtDSAssembly>().ToTable("ETL_ExtDSAssembly");
            modelBuilder.Entity<EExtDSAssembly>().HasKey(p => p.Id);

            modelBuilder.Entity<EExtDSType>().ToTable("ETL_ExtDSType");
            modelBuilder.Entity<EExtDSType>().HasKey(p => p.Id);

            modelBuilder.Entity<EAppExtractRecord>().ToTable("ETL_AppExtractRecord");
            modelBuilder.Entity<EAppExtractRecord>().HasKey(p => p.Id);

            modelBuilder.Entity<Views.VAppExtractRecord>().ToTable("ETL_V_AppExtractRecord");
            modelBuilder.Entity<Views.VAppExtractRecord>().HasKey(p => p.Id);

            modelBuilder.Entity<EAppExtractRejectRecord>().ToTable("ETL_AppExtractRejectRecord");
            modelBuilder.Entity<EAppExtractRejectRecord>().HasKey(p => p.Id);
            modelBuilder.Entity<EAppExtractReceiveRecord>().ToTable("ETL_AppExtractReceiveRecord");
            modelBuilder.Entity<EAppExtractReceiveRecord>().HasKey(p => p.Id);

            modelBuilder.Entity<Views.VTask>().ToTable("ETL_V_Task");
            modelBuilder.Entity<Views.VTask>().HasKey(p => p.Id);

            modelBuilder.Entity<EPubDataSource>().ToTable("ETL_PubDataSource");
            modelBuilder.Entity<EPubDataSource>().HasKey(p => p.Id);

            modelBuilder.Entity<EPubParameter>().ToTable("ETL_PubParameter");
            modelBuilder.Entity<EPubParameter>().HasKey(p => p.Id);

            modelBuilder.Entity<ExtensionApp>().ToTable("ETL_ExtensionApp");
            modelBuilder.Entity<ExtensionApp>().HasKey(p => p.Id);

            //提醒记录 包含短信、邮件
            modelBuilder.Entity<EMessageAlertRecord>().ToTable("ETL_MessageAlertRecord");
            modelBuilder.Entity<EMessageAlertRecord>().HasKey(p => p.Id);



            // 数据完整性验证的插件参数
            modelBuilder.Entity<IntegrityCheckInstance>().ToTable("ETL_IntegrityCheckInstance");
            modelBuilder.Entity<IntegrityCheckInstance>().HasKey(p => p.Id);
            modelBuilder.Entity<IntegrityCheckParameter>().ToTable("ETL_IntegrityCheckParameter");
            modelBuilder.Entity<IntegrityCheckParameter>().HasKey(p => p.Id);
            modelBuilder.Entity<Maintainer>().ToTable("ETL_Maintainer");
            modelBuilder.Entity<Maintainer>().HasKey(p => p.Id);

            #region views

            modelBuilder.Entity<Views.VDtTableColumn>().ToTable("ETL_V_DtTableColumn");
            modelBuilder.Entity<Views.VDtTableColumn>().HasKey(p => p.Id);

            modelBuilder.Entity<Views.VDtSourceDataIdentity>().ToTable("ETL_V_DtSourceDataIdentity");
            modelBuilder.Entity<Views.VDtSourceDataIdentity>().HasKey(p => new { p.DtSerId, p.DtSourceId, p.DtSourceColumnId });

            modelBuilder.Entity<Views.VBatchParameter>().ToTable("ETL_V_BatchParameter");
            modelBuilder.Entity<Views.VBatchParameter>().HasKey(p => p.Id);

            modelBuilder.Entity<Views.VCodeConversionRule>().ToTable("ETL_V_CodeConversionRule");
            modelBuilder.Entity<Views.VCodeConversionRule>().HasKey(p => p.Id);

            modelBuilder.Entity<Views.VEventModeTask>().ToTable("ETL_V_EventModeTask");
            modelBuilder.Entity<Views.VEventModeTask>().HasKey(p => p.Id);

            modelBuilder.Entity<Views.VDVPlugIn>().ToTable("ETL_V_DVPlugIn");
            modelBuilder.Entity<Views.VDVPlugIn>().HasKey(p => p.Id);

            #endregion

            //modelBuilder.Entity<DataSourceType>().Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        }

        public override int SaveChanges()
        {
            try
            {
                int result = base.SaveChanges();
                // _transaction.Commit();
                return result;
            }
            catch (Exception ex)
            {
                // _transaction.Rollback();
                throw ex;
            }
        }
    }
    public static class EFExt
    {
        public static void Remove<TSource>(this IEnumerable<TSource> set, Expression<Func<TSource, bool>> predicate)
        {
            Type type = predicate.GetType();
        }
    }
}
